package sft.BancodeDados;

import sft.Logica.Insumos;
import java.sql.*;
import java.util.*;

public class LogicaQuery {
    private static Connection conexao;

    public LogicaQuery() throws SQLException {
        LogicaQuery.conexao = new CriaConexao().getConnection();
    }
    
    public List<Insumos> getList(Insumos l1) throws SQLException{
        
        PreparedStatement stmt = conexao.prepareStatement(l1.getSql());
        switch (l1.getCaso()) {
            case 1:
                stmt.setString(1, "%" + l1.getNome() + "%");
                break;
            default:
                break;
        }
        return setSTMT(stmt);
    }

    private List<Insumos> setSTMT(PreparedStatement stmt) throws SQLException {
        ResultSet rs = stmt.executeQuery();
        
        List<Insumos> list = new ArrayList<>();
        
        while (rs.next()) {            
            Insumos l = new Insumos();
            l.setId(rs.getInt(1));
            l.setNome(rs.getString(2));
            list.add(l);
        }
        stmt.close();
        rs.close();
        return list;
    }
}
